var gulp = require('gulp');
var browserSync = require('browser-sync');
var gulpfile = require('gulp-file-include');
var htmlmin = require('gulp-htmlmin');
var cleanCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var imagemin = require('gulp-imagemin');

/*html压缩*/
gulp.task('htmlmin', function () {
    var options = {
        removeComments: true,//清除HTML注释
        collapseWhitespace: true,//压缩HTML
        collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        minifyJS: true,//压缩页面JS
        minifyCSS: true//压缩页面CSS
    };
    return gulp.src('src/**/*.php')
        .pipe(htmlmin(options))
        .pipe(gulp.dest('./dist'));
});

gulp.task('browser-sync', function () {
  browserSync.init({
      files:['./src'],
      server:{
          baseDir:'./src',  // 设置服务器的根目录
          index:'./index.php' // 指定默认打开的文件
      },
      port:3000
  });

});
gulp.task('imagesmin', function () {
    gulp.src('src/images/**/*.*')
        .pipe(imagemin({
            optimizationLevel: 4 //类型：Number  默认：3  取值范围：0-7（优化等级）
        }))
        .pipe(gulp.dest('dist/images'))
});

gulp.task('cssmin', function () {
    gulp.src('src/css/*.css')
        .pipe(cleanCSS({
            advanced: true,//类型：Boolean 默认：true [是否开启高级优化（合并选择器等）]
            compatibility: 'ie7',//保留ie7及以下兼容写法 类型：String 默认：''or'*' [启用兼容模式； 'ie7'：IE7兼容模式，'ie8'：IE8兼容模式，'*'：IE9+兼容模式]
            keepSpecialComments: '*'
        }))
        .pipe(gulp.dest('dist/css'));
});

gulp.task('jsmin', function () {
    gulp.src('src/*.js')
        .pipe(uglify())
        .pipe(gulp.dest('dist'));
    gulp.src('src/js/*.js')
    .pipe(uglify())
    .pipe(gulp.dest('dist/js'));

});
gulp.task('copy', function () {
    gulp.src('src/data/*.*')
    .pipe(gulp.dest('dist/data'));
    gulp.src('src/info/*.*')
    .pipe(gulp.dest('dist/info'));
    gulp.src('src/*.ico')
    .pipe(gulp.dest('dist'));

});
gulp.task('dev', ['htmlmin','jsmin','imagesmin','cssmin','copy']);


gulp.task('default', ['browser-sync']);
//'include','htmlmin',
